<template>
  <div class="movies">
    <router-view></router-view>
    <ul>
      <li
        v-for="(item, index) in moviesList"
        :key="index"
        @click="gotoDetail(item.movieid)"
      >
        <div class="avatar">
          <img :src="item.img | getImgUrl" alt="" />
        </div>
        <div class="movie-info">
          <div class="movie-content">
            <div class="movie-title">{{ item.nm }}</div>
            <p class="movie-evaluate" v-if="item.showst == 3">
              <span>观众评: </span>
              <i>{{ item.sc }}</i>
            </p>
            <p class="movie-evaluate" v-else-if="item.showst == 4">
              <i>{{ item.wish }}</i>
              <span> 人想看</span>
            </p>
            <p class="movie-star">主演: {{ item.star }}</p>
            <p class="movie-show">{{ item.showInfo }}</p>
          </div>
          <div class="btn-buy">
            <div class="buy" v-if="item.showst == 3">购票</div>
            <div class="presale" v-else-if="item.showst == 4">预售</div>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
// import { getMoviesDataApi, getPullDataApi } from "@/utils/api";

import { mapState, mapActions } from "vuex";

export default {
  props: {
    pullUp: Number,
  },
  data() {
    return {
      // moviesList: [],
      // movieIds: [],
      // // 从第几条id开始截取后传送给后端
      // startId: 0,
      // // 每次需要加载的数据条数
      // size: 5,
    };
  },
  // computed: mapState(["moviesList", "movieIds", "startId", "size"]),
  computed: mapState({
    moviesList: (state) => state.movies.moviesList,
    movieIds: (state) => state.movies.movieIds,
    startId: (state) => state.movies.startId,
    size: (state) => state.movies.size,
  }),
  mounted() {
    // this.getMoviesData();
    this.getMoviesDataAsync().then(async () => {
      await this.$nextTick();
      this.$emit("finishRender");
    });
  },
  methods: {
    ...mapActions(["getMoviesDataAsync", "getPullDataAsync"]),
    // async getMoviesData() {
    // fetch("http://www.pudge.wang:3080/api/movies/list")
    //   .then((response) => response.json())
    //   .then((res) => {
    //     if (res.status === 0) {
    //       this.moviesList = res.result;
    //     }
    //   });

    // const res = await getMoviesDataApi();
    // console.log(res.result);
    // this.moviesList = res.result;
    // this.movieIds = res.ids;
    // this.startId = res.result.length;

    //   await this.$nextTick();
    //   this.$emit("finishRender");
    // },

    gotoDetail(index) {
      this.$router.push("/detail/" + index);
    },
  },
  watch: {
    async pullUp() {
      // 截取需要上拉加载的数据id值都有那几条
      const arr = this.movieIds.slice(this.startId, this.startId + this.size);
      // 将数组转化为字符串形式，由于传过去的数据规定需要是字符串形式
      const str = arr.join(",");

      // const res = await getPullDataApi({ ids: str });

      // 之前的数据拼接上新加载的数据 (注意: concat不改变原数组所以需要重新赋值)
      // this.moviesList = this.moviesList.concat(res.result);
      // console.log(this.moviesList);
      // 注意: 每次上拉加载完成后将起始id值进行改变，加上这次上拉加载的长度
      // this.startId += this.size;

      this.getPullDataAsync({ ids: str }).then(async () => {
        await this.$nextTick();
        // 通知父组件数据加载完成
        this.$emit(
          "dataUpdate",
          this.moviesList.length >= this.movieIds.length
        );
      });

      // await this.$nextTick();
      // // 通知父组件数据加载完成
      // this.$emit("dataUpdate", this.moviesList.length >= this.movieIds.length);
    },
  },
  filters: {
    getImgUrl(value) {
      return value.replace("/w.h", "");
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../assets/css/commons.less";
.movies {
  width: 100%;
  background-color: #fff;

  li {
    display: flex;
    width: 100%;
    padding: 0 15px;
    height: 114px;

    .avatar {
      width: 64px;
      height: 90px;
      margin-top: 12px;
      img {
        width: 100%;
        height: 100%;
      }
    }

    .movie-info {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      padding-right: 14px;
      height: 100px;
      max-height: 100px;
      margin-top: 12px;
      margin-left: 14px;
      border-bottom: 1px solid @border-color;

      .movie-content {
        flex: 1;
        width: 202px;
        padding-right: 5px;
        overflow: hidden;
      }

      .movie-title {
        line-height: 24px;
        margin-bottom: 7px;
        font-size: @fontSize_18;
        color: #333;
        font-weight: 700;
        flex-shrink: 1;
        width: 100%;
        .overflow_hidden();
      }

      .movie-evaluate {
        .movie_common();
        .overflow_hidden();
        i {
          font-weight: 700;
          color: #faaf00;
          font-size: @fontSize_16;
        }
      }

      .movie-star,
      .movie-show {
        width: 100%;
        .movie_common();
        .overflow_hidden();
        margin-top: 6px;
        line-height: 15px;
      }

      .btn-buy {
        width: 47px;
        .center();
        .buy,
        .presale {
          width: 47px;
          height: 28px;
          line-height: 28px;
          text-align: center;
          font-size: @fontSize_12;
          color: #fff;
          border-radius: 4px;
          cursor: pointer;
        }
        .buy {
          background-color: #f03d37;
        }
        .presale {
          background: #3c9fe6;
        }
      }
    }
  }
}
</style>
